﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Runtime.InteropServices

Namespace System.Workflow.ComponentModel.Design
    Public Class CompositeDesignerTheme
        Inherits ActivityDesignerTheme
        ' Methods
        Public Sub New(ByVal theme As WorkflowTheme)
            MyBase.New(theme)
            Me.watermarkAlignment = DesignerContentAlignment.BottomRight
            Me.watermarkImagePath = String.Empty
            Me.endCap = LineAnchor.ArrowAnchor
            Me.expandButtonRectangle = Rectangle.Empty
        End Sub

        Private Sub ApplySystemColors()
            Me.BackColorStart = Color.Empty
            Me.BackColorEnd = Color.Empty
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try 
                If (Not Me.expandButtonBackBrush Is Nothing) Then
                    Me.expandButtonBackBrush.Dispose
                    Me.expandButtonBackBrush = Nothing
                End If
                If (Not Me.watermarkImage Is Nothing) Then
                    Me.watermarkImage.Dispose
                    Me.watermarkImage = Nothing
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Public Function GetExpandButtonBackgroundBrush(ByVal rectangle As Rectangle) As Brush
            If ((Me.expandButtonBackBrush Is Nothing) OrElse (Me.expandButtonRectangle <> rectangle)) Then
                If (Not Me.expandButtonBackBrush Is Nothing) Then
                    Me.expandButtonBackBrush.Dispose
                End If
                Me.expandButtonRectangle = rectangle
                Me.expandButtonBackBrush = New LinearGradientBrush(Me.expandButtonRectangle, Color.White, Color.FromArgb(&HAD, 170, &H9C), LinearGradientMode.ForwardDiagonal)
            End If
            Return Me.expandButtonBackBrush
        End Function

        Public Overrides Sub Initialize()
            MyBase.Initialize
            If MyBase.ContainingTheme.AmbientTheme.UseOperatingSystemSettings Then
                Me.ApplySystemColors
            End If
        End Sub

        Public Overrides Sub OnAmbientPropertyChanged(ByVal ambientProperty As AmbientProperty)
            MyBase.OnAmbientPropertyChanged(ambientProperty)
            If (ambientProperty = AmbientProperty.OperatingSystemSetting) Then
                Me.ApplySystemColors
            End If
        End Sub


        ' Properties
        <DefaultValue(2), SRDescription("ConnectorEndCapDesc", "System.Workflow.ComponentModel.Design.DesignerResources"), DispId(12), SRCategory("ForegroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources")> _
        Public Overridable Property ConnectorEndCap As LineAnchor
            Get
                Return Me.endCap
            End Get
            Set(ByVal value As LineAnchor)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.endCap = value
            End Set
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public Overridable ReadOnly Property ConnectorSize As Size
            Get
                If ((Not Me.DesignerType Is Nothing) AndAlso GetType(FreeformActivityDesigner).IsAssignableFrom(Me.DesignerType)) Then
                    Dim height As Integer = CompositeDesignerTheme.ConnectorSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize)).Height
                    Return New Size(height, height)
                End If
                Return CompositeDesignerTheme.ConnectorSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize))
            End Get
        End Property

        <SRDescription("ConnectorStartCapDesc", "System.Workflow.ComponentModel.Design.DesignerResources"), SRCategory("ForegroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), DefaultValue(0), DispId(11)> _
        Public Overridable Property ConnectorStartCap As LineAnchor
            Get
                Return Me.startCap
            End Get
            Set(ByVal value As LineAnchor)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.startCap = value
            End Set
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public Overridable ReadOnly Property ExpandButtonSize As Size
            Get
                Return CompositeDesignerTheme.ExpandButtonSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize))
            End Get
        End Property

        <DispId(10), SRCategory("BackgroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), DefaultValue(False), SRDescription("DropShadowDesc", "System.Workflow.ComponentModel.Design.DesignerResources")> _
        Public Overridable Property ShowDropShadow As Boolean
            Get
                Return Me.dropShadow
            End Get
            Set(ByVal value As Boolean)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.dropShadow = value
            End Set
        End Property

        <SRCategory("BackgroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), DispId(9), DefaultValue(12), SRDescription("WatermarkAlignmentDesc", "System.Workflow.ComponentModel.Design.DesignerResources")> _
        Public Overridable Property WatermarkAlignment As DesignerContentAlignment
            Get
                Return Me.watermarkAlignment
            End Get
            Set(ByVal value As DesignerContentAlignment)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.watermarkAlignment = value
            End Set
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Public ReadOnly Property WatermarkImage As Image
            Get
                If ((Me.watermarkImage Is Nothing) AndAlso (Me.watermarkImagePath.Length > 0)) Then
                    Me.watermarkImage = DesignerHelpers.GetImageFromPath(Me, MyBase.ContainingTheme.ContainingFileDirectory, Me.watermarkImagePath)
                End If
                Return Me.watermarkImage
            End Get
        End Property

        <SRCategory("BackgroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), DispId(8), SRDescription("WatermarkDesc", "System.Workflow.ComponentModel.Design.DesignerResources"), Editor(GetType(ImageBrowserEditor), GetType(UITypeEditor))> _
        Public Overridable Property WatermarkImagePath As String
            Get
                Return Me.watermarkImagePath
            End Get
            Set(ByVal value As String)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                If ((Not String.IsNullOrEmpty(value) AndAlso value.Contains(Path.DirectorySeparatorChar.ToString)) AndAlso Path.IsPathRooted(value)) Then
                    value = DesignerHelpers.GetRelativePath(MyBase.ContainingTheme.ContainingFileDirectory, value)
                    If Not DesignerHelpers.IsValidImageResource(Me, MyBase.ContainingTheme.ContainingFileDirectory, value) Then
                        Throw New InvalidOperationException(DR.GetString("Error_InvalidImageResource", New Object(0  - 1) {}))
                    End If
                End If
                Me.watermarkImagePath = value
                If (Not Me.watermarkImage Is Nothing) Then
                    Me.watermarkImage.Dispose
                    Me.watermarkImage = Nothing
                End If
            End Set
        End Property


        ' Fields
        Private Shared ReadOnly ConnectorSizes As Size() = New Size() { New Size(15, 30), New Size(15, &H13), New Size(&H19, 50) }
        Private dropShadow As Boolean
        Private endCap As LineAnchor
        Private expandButtonBackBrush As Brush
        Friend Shared ReadOnly ExpandButtonBorderPen As Pen = New Pen(Color.FromArgb(&H7B, &H9A, &HB5), 1!)
        Friend Shared ReadOnly ExpandButtonForegoundPen As Pen = New Pen(Color.Black, 1!)
        Private expandButtonRectangle As Rectangle
        Private Shared ReadOnly ExpandButtonSizes As Size() = New Size() { New Size(8, 8), New Size(8, 8), New Size(12, 12) }
        Private startCap As LineAnchor
        Private watermarkAlignment As DesignerContentAlignment
        Private watermarkImage As Image
        Private watermarkImagePath As String
    End Class
End Namespace

